import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from Entities import *

def Add(deviceName):
    device = db.GqlQuery("SELECT * FROM Device WHERE DeviceName =:1",deviceName)
    if device.count() == 0:
        device = Device(DeviceName = deviceName)
        device.put()
        return 1
    else:
        return 0
        
def Delete(deviceName):
    device = db.GqlQuery("SELECT * FROM Device WHERE DeviceName =:1",deviceName)
    if device.count() == 0:
        return 0
    else:
        Device.delete(device)
        return 1

def Get():
    device_list = []
    devices = db.GqlQuery("SELECT * FROM Device")
    for device in devices:
        device_dict = {}
        device_dict["DeviceName"] = device.DeviceName
        device_dict["DeviceID"] = device.key().id()
        device_list.append(device_dict)
    tuple_device = tuple(device_list)
    return tuple_device